/*
 * @Author: ljc
 * @Date: 2023-04-27 13:26:58
 * @LastEditors: g05047
 * @LastEditTime: 2023-04-27 13:56:20
 * @Description: 关于文件工具方法
 * 
 */
// 文件下载
export function dowloadFile(data, fileName = "") {
    console.log(data)
    let blob = new Blob([data]);
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(blob, fileName);
    } else {
        let objectUrl = (window.URL || window.webkitURL).createObjectURL(
            blob
        );
        let downFile = document.createElement("a");
        downFile.style.display = "none";
        downFile.href = objectUrl;
        downFile.download = fileName; // 下载后文件名
        document.body.appendChild(downFile);
        downFile.click();
        document.body.removeChild(downFile); // 下载完成移除元素 // window.location.href = objectUrl
        window.URL.revokeObjectURL(objectUrl); // 只要映射存在，Blob就不能进行垃圾回收，因此一旦不再需要引用，就必须小心撤销URL，释放掉blob对象。
    }
}
// 文件预览类型
export const fileType = {
    pdf: 'application/pdf',
    jpeg: 'image/jpeg',
    png: 'image/png',
    gif: 'image/gif',
    mp3: 'audio/mpeg',
    svg: 'image/svg+xml',
    text: 'text/plain',
    bmp: 'image/bmp'
}
// 文件预览
export function previewFile(data, type) {
    console.log(data)
    let blob = new Blob([data], { type });

    let url = (window.URL || window.webkitURL).createObjectURL(blob)
    return url
}

